﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;
using System.IO;

namespace WebHousingInfo
{
    public partial class TimKiemNangCao : System.Web.UI.Page
    {
        public static NewsHouseBOList newshousebolist;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getCategoryHouse();
                getHouseType();
                getAreas();
                currentcylist();
            }
        }

        private void getCategoryHouse()
        {
            try
            {
                var categoryHouse = CategoryInfo.GetCategoryInfo_BySingle(true, false, false, false, true);
                dlcategory.DataSource = categoryHouse;
                dlcategory.DataBind();
                dlcategory.Items.Insert(0, "Tất cả");
                dlcategory.SelectedIndex = 0;
            }
            catch { }
        }

        private void getHouseType()
        {
            try
            {
                var housingType = HousingTypeInfo.GetHousingTypeInfo();
                dlhousetype.DataSource = housingType;
                dlhousetype.DataBind();
                dlhousetype.Items.Insert(0, "Tất cả");
                dlhousetype.SelectedIndex = 0;
            }
            catch { }
        }

        private void getAreas()
        {
            try
            {
                var areas = AreaInfo.GetAreaInfo();
                if (areas != null)
                {
                    dlareas.DataSource = areas;
                    dlareas.DataBind();
                    dlareas.Items.Insert(0, "Tất cả");
                    dlareas.SelectedIndex = 0;
                    //lay danh sach khu vuc con
                    var areaId = dlareas.SelectedValue;
                    getAreaDetailWithAreaId(areaId);
                }
            }
            catch { }
        }
        private void getAreaDetailWithAreaId(string areaId)
        {
            try
            {
                if (areaId != "Tất cả")
                {
                    var areadetailList = AreaDetailInfo.GetAreaDetailInfo(int.Parse(areaId));
                    dlareasdetail.DataSource = areadetailList;
                    dlareasdetail.DataBind();
                    dlareasdetail.Items.Insert(0, "Tất cả");
                    dlareasdetail.SelectedIndex = 0;
                }
                else
                {
                    dlareasdetail.Items.Clear();
                    dlareasdetail.Items.Add("Tất cả");
                }
            }
            catch
            {
            }
        }
        private void currentcylist()
        {
            try
            {
                var currentcy = CurrencyInfo.GetCurrencyInfo();
                dlcurrency.DataSource = currentcy;
                dlcurrency.DataBind();
                dlcurrency.Items.Insert(0, "Tất cả");
                dlcurrency.SelectedIndex = 0;
            }
            catch
            { }
        }

        protected void dlareas_SelectedIndexChanged(object sender, EventArgs e)
        {
            var areaId = dlareas.SelectedValue;
            getAreaDetailWithAreaId(areaId);
        }

        public NewsHouseBOList ReturnListSearch()
        {
            try
            {
                if (newshousebolist.Count > 0)
                    return newshousebolist;
                else return null;
            }
            catch
            {
                return null;
            }
        }

        protected void btnsearchadvaned_Click(object sender, EventArgs e)
        {
            string cate, area, areadetail, housetype, totalsure, price, currentcy, direction, doccument, sort, anykey ;
            bool supm;
            if (dlcategory.SelectedValue == "Tất cả")
                cate = null;
            else cate = dlcategory.SelectedValue;
            if (dlhousetype.SelectedValue == "Tất cả")
                housetype = null;
            else housetype = dlhousetype.SelectedValue;
            if (dlareas.SelectedValue == "Tất cả")
                area = null;
            else area = dlareas.SelectedValue;
            if (dlareasdetail.SelectedValue == "Tất cả")
                areadetail = null;
            else areadetail = dlareasdetail.SelectedValue;
            if (dlsure.SelectedValue == "all")
                totalsure = null;
            else totalsure = dlsure.SelectedValue;
            price = dlprice.SelectedValue;
            supm = cbsupm.Checked;
            if (dlcurrency.SelectedValue == "Tất cả")
                currentcy = null;
            else currentcy = dlcurrency.SelectedValue;
            if (dldirection.SelectedValue == "Tất cả")
                direction = null;
            else direction = dldirection.SelectedValue;
            if (dldocument.SelectedValue == "Tất cả")
                doccument = null;
            else doccument = dldocument.SelectedValue;
            sort = dlsort.SelectedValue;
            if (txtanykey.Text.Trim() == "")
                anykey = null;
            else anykey = txtanykey.Text;
            newshousebolist = NewsHouseBOList.GetNewsHouse_BySearchAdvand(cate, area, areadetail, housetype, totalsure, price, supm, currentcy, direction, doccument, sort, anykey);
        }

        public bool checkurlimage(string imagestring)
        {
            var pathFileImage = Server.MapPath("TotalUpload/ImagesTotal/" + imagestring);
            if (File.Exists(pathFileImage))
            {
                return true;
            }
            else return false;
        }

        protected void dlsapxep_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (dlsapxep.SelectedValue == "ngaydang")
            //    newshousebolist.OrderByDescending(a => a.CreatedDate);
            //else if (dlsapxep.SelectedValue == "xemnhieu")
            //    newshousebolist.OrderByDescending(a => a.ViewCount);
            //else if (dlsapxep.SelectedValue == "giamax")
            //    newshousebolist.OrderByDescending(a => a.Price);
            //else if (dlsapxep.SelectedValue == "giamin")
            //    newshousebolist.OrderBy(a => a.Price);
        }
    }
}
